package com.useeinfo.oa.modules.base.entity;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.useeinfo.framework.extend.entity.SuperClassEntity;
import com.useeinfo.framework.sugar.tools.StringConverters;
import com.useeinfo.oa.modules.security.util.SessionKeyUtils;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.persistence.*;
import javax.servlet.http.HttpServletRequest;
import java.util.HashSet;
import java.util.Set;

/**
 * 人员表
 */
@Entity
@Table(name = "personnel")
public class Personnel extends SuperClassEntity<Personnel> {

    /**
     * 人员名称
     */
    private String name;
    /**
     * 手机
     */
    private String mobile;
    /**
     * 性别
     */
    private String sex;
    /**
     * 邮箱
     */
    private String email;

    /**
     * 员工编号
     */
    private String employeeCode;
    /**
     * 所属企业
     */
    @ManyToMany()
    @JoinTable(name = "personnel_2_enterprise", joinColumns = @JoinColumn(name = "personnel_id"), inverseJoinColumns = @JoinColumn(name = "enterprise_id"))
    private Set<Enterprise> enterpriseSet = new HashSet<>();
    /**
     * 所属岗位
     */
    @ManyToMany()
    @JoinTable(name = "personnel_2_position", joinColumns = @JoinColumn(name = "personnel_id"), inverseJoinColumns = @JoinColumn(name = "position_id"))
    private Set<Position> positionSet = new HashSet<>();
    /**
     * 所属区域
     */
    @ManyToMany()
    @JoinTable(name = "personnel_2_area", joinColumns = @JoinColumn(name = "personnel_id"), inverseJoinColumns = @JoinColumn(name = "area_id"))
    private Set<Area> areaSet = new HashSet<>();

    @OneToMany(mappedBy = "personnel")
    private Set<PersonnnelOrganization>personnnelOrganizationSet = new HashSet<>();

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getMobile() {
        return mobile;
    }

    public void setMobile(String mobile) {
        this.mobile = mobile;
    }

    public String getSex() {
        return sex;
    }

    public void setSex(String sex) {
        this.sex = sex;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }


    public Set<Position> getPositionSet() {
        return positionSet;
    }

    public void setPositionSet(Set<Position> positionSet) {
        this.positionSet = positionSet;
    }

    public String getEmployeeCode() {
        return employeeCode;
    }

    public void setEmployeeCode(String employeeCode) {
        this.employeeCode = employeeCode;
    }

    public Set<Enterprise> getEnterpriseSet() {
        return enterpriseSet;
    }

    public void setEnterpriseSet(Set<Enterprise> enterpriseSet) {
        this.enterpriseSet = enterpriseSet;
    }

    public Set<Area> getAreaSet() {
        return areaSet;
    }

    public void setAreaSet(Set<Area> areaSet) {
        this.areaSet = areaSet;
    }

    public Set<PersonnnelOrganization> getPersonnnelOrganizationSet() {
        return personnnelOrganizationSet;
    }

    public void setPersonnnelOrganizationSet(Set<PersonnnelOrganization> personnnelOrganizationSet) {
        this.personnnelOrganizationSet = personnnelOrganizationSet;
    }

    @Override
    public JSONObject toJSONObject() {
        JSONObject jsonObject = super.toJSONObject();
        jsonObject.put("name", StringConverters.null2String(getName()));
        jsonObject.put("email", StringConverters.null2String(getEmail()));
        jsonObject.put("employeeCode", StringConverters.null2String(getEmployeeCode()));
        jsonObject.put("mobile", StringConverters.null2String(getMobile()));
        jsonObject.put("sex", StringConverters.null2String(getSex()));

        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        Enterprise enterprise = SessionKeyUtils.getSessionEnterprise(request);
        Set<Position>positions = getPositionSet();
        Position position = getPosition(enterprise, getPositionSet());
        if (null != position) {
            jsonObject.put("position", StringConverters.null2String(position.getName()));
            jsonObject.put("positionId", position.getId());
        } else {
            jsonObject.put("position", "");
            jsonObject.put("positionId", "");
        }
        PersonnnelOrganization personnnelOrganization = getOrganization(enterprise, getPersonnnelOrganizationSet());
        if (null != personnnelOrganization) {
            if(null!=personnnelOrganization.getOrganization()) {
                jsonObject.put("organization", StringConverters.null2String(personnnelOrganization.getOrganization().getOrganizationName()));
                jsonObject.put("organizationId", personnnelOrganization.getOrganization().getId());
            }else {
                jsonObject.put("organization", "");
                jsonObject.put("organizationId", "");
            }
            jsonObject.put("leader",personnnelOrganization.isLeader());
        } else {
            jsonObject.put("organization", "");
            jsonObject.put("organizationId", "");
            jsonObject.put("leader",false);
        }
        JSONArray jsonArray = getAreaId(enterprise,getAreaSet());
        jsonObject.put("areaIds", jsonArray);
        return jsonObject;
    }

    public JSONArray getAreaId(Enterprise enterprise,Set<Area>areaSet){
        JSONArray jsonArray = new JSONArray();
        for(Area area:areaSet){
            if (area.getEnterprise().getId().equals(enterprise.getId())) {
                jsonArray.add(area.getId().toString());
            }
        }
        return jsonArray;
    }

    public Position getPosition(Enterprise enterprise, Set<Position> positionSet) {
        for (Position position : positionSet) {
            if (position.getEnterprise().getId().equals(enterprise.getId())) {
                return position;
            }
        }
        return null;
    }
    public PersonnnelOrganization getOrganization(Enterprise enterprise,Set<PersonnnelOrganization>personnnelOrganizationSet){
        for (PersonnnelOrganization personnnelOrganization : personnnelOrganizationSet) {
            if (null!=personnnelOrganization.getOrganization()&&personnnelOrganization.getOrganization().getEnterprise().getId().equals(enterprise.getId())) {
                return personnnelOrganization;
            }
        }
        return null;
    }

    @Override
    public boolean equals(Object obj) {
        if(obj == null) return false;
        if(this == obj) return true;
        if(obj instanceof Personnel){
            Personnel personnel = (Personnel)obj;
            return personnel.getId().equals(this.getId());
        }
        return false;
    }
    @Override
    public int hashCode() {
        // TODO Auto-generated method stub
        //return name.hashCode();
        return getId().hashCode();//如果没有对象的唯一值，可使用 return  name.hashCode()
    }

}
